const tokens = [
  {
    username: 'admin',
    password: '111111',
    email: '863450594@qq.com',
    realname: 'aaa',
    telephone: 13230970751,
    token: 'admin-token'
  },
  {
    username: 'xiaoming',
    password: '123456',
    email: '863450594@qq.com',
    realname: 'aaa',
    telephone: 13230970751,
    token: 'admin-token'
  },
  {
    username: 'editor',
    password: '111111',
    email: '2185367837@qq.com',
    realname: 'bbb',
    telephone: 18833983651,
    token: 'editor-token'
  }
]

const users = {
  'admin-token': {
    id: 1,
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    rolename: '管理员',
    name: 'admin',
    sex: '男',
    enter_id: '皮燕加工厂',
    hire_date: '2021.06.22T15:44:22'
  },
  'editor-token': {
    id: 2,
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    rolename: '普通用户',
    username: 'editor',
    sex: '女',
    enter_id: '托里噶工厂',
    hire_date: '2021.06.23T15:44:22'
  }
}

module.exports = [
  // user login
  {
    url: '/user/login',
    type: 'post',
    response: config => {
      const { username, password } = config.body
      let token = null
      for (let i = 0; i < tokens.length; i++) {
        if (tokens[i].username === username && tokens[i].password === password) {
          token = tokens[i]
          break
        }
      }

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: '用户名或密码不正确'
        }
      }

      return {
        code: 200,
        obj: token.token
      }
    }
  },
  {
    url: '/user/register',
    type: 'post',
    response: config => {
      const { username, password, email, telephone, realname } = config.body

      for (let i = 0; i < tokens.length; i++) {
        if (tokens[i].username === username) {
          return {
            code: 60204,
            message: '用户名已被注册'
          }
        }
      }
      tokens.push({ username: username, password: password, email: email, realname: realname, telephone: telephone, token: 'admin-token' })
      return {
        code: 200,
        data: 'admin-token'
      }
    }
  },
  {
    url: '/user/exists',
    type: 'post',
    response: config => {
      const { info } = config.body
      const token = tokens[info]
      // mock error
      // 判断用户是否已经被注册
      if (!token) {
        return {
          code: 60204,
          message: '用户名不存在'
        }
      }

      return {
        code: 200
      }
    }
  },
  {
    url: '/user/resetpwd',
    type: 'post',
    response: config => {
      console.log('receive request')
      return {
        code: 200
      }
    }
  },

  // get user info
  {
    url: '/user/info\.*',
    type: 'get',
    response: config => {
      console.log(config.query)
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: '无法拉取用户信息'
        }
      }
      console.log(info)
      return {
        code: 200,
        obj: info
      }
    }
  },

  // user logout
  {
    url: '/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 200,
        data: 'success'
      }
    }
  }
]
